package com.fubowen.reader.adapter

import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.fubowen.reader.R
import com.fubowen.reader.databinding.PublicTreeItemBinding
import com.fubowen.reader.extension.setClipDrawable
import com.fubowen.reader.extension.setLeftDrawable
import com.fubowen.reader.extension.setRightDrawable
import com.fubowen.reader.extension.setVisible
import com.fubowen.reader.db.model.Tree
import com.fubowen.reader.vm.ThemeViewModel


class TreeAdapter(val context: Context, val theme: ThemeViewModel, val moreButtonText: String) : ListAdapter<Tree, TreeAdapter.ViewHolder>(DiffCallback()) {

    //region onCreateViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            PublicTreeItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }
    //endregion

    //region setOnClickListener
    private var onClickListener: OnClickListener? = null
    fun setOnClickListener(listener: OnClickListener) {
        onClickListener = listener
    }
    //endregion

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        if (item != null) {
            holder.icon.setImageResource(item.icon)
            holder.name.text = item.name
            holder.time.text = item.time
            holder.size.text = item.size
            if(!item.errMsg.isNullOrBlank()){
                holder.name.text = item.errMsg
            }

            //region 文件还是文件夹

            when (item.type) {
                Tree.TYPE_FOLDER -> {
                    holder.time.setVisible(false)
                    holder.size.setVisible(false)
                    holder.childrenCount.setVisible(true)
                    holder.childrenCount.text = context.getString(R.string.childrenCount, item.children.size)
                }

                Tree.TYPE_FILE -> {
                    holder.time.setVisible(true)
                    holder.size.setVisible(true)
                    holder.childrenCount.setVisible(false)
                }
            }

            //endregion

            when (item.status) {

                Tree.STATUS_NOT_START -> {
                    val id = if (item.isChecked) R.drawable.ic_check_circle_24dp else 0
                    val color = theme.getColorOfCheckedState(item.isChecked)
                    holder.button.setLeftDrawable(id, color)
                    if (moreButtonText.isNotBlank()) {
                        holder.button.text = moreButtonText
                        holder.button.setBackgroundResource(R.drawable.border_round_4dp)
                        holder.button.setPadding(24, 12, 24, 12)
                    }
                }

                Tree.STATUS_RUNNING -> {
                    holder.itemView.setClipDrawable(item.progress, theme.getProgressBarColor())
                    holder.button.text = context.getString(R.string.progressTip, item.progress)
                    holder.button.setRightDrawable(R.drawable.ic_pause_circle_24dp, Color.GREEN)
                }

                Tree.STATUS_PAUSED -> {
                    holder.itemView.setClipDrawable(item.progress, theme.getProgressBarColor())
                    holder.button.text = context.getString(R.string.progressTip, item.progress)
                    holder.button.setRightDrawable(R.drawable.ic_play_24dp, Color.RED)
                }

                Tree.STATUS_COMPLETED -> {
                    holder.itemView.setBackgroundColor(theme.getBackgroundColor())
                    holder.button.text = "已完成"
                    holder.button.setRightDrawable()
                }
            }
        }
    }

    //region ViewHolder
    inner class ViewHolder(binding: PublicTreeItemBinding) : RecyclerView.ViewHolder(binding.root) {

        val icon = binding.icon
        val name = binding.name
        val time = binding.time
        val size = binding.size
        val button = binding.button
        val childrenCount = binding.childrenCount

        init {
            binding.root.setOnClickListener { onClickListener?.onItemClick(it) }
            button.setOnClickListener { onClickListener?.onSelectedClick(binding.root) }
        }
    }
    //endregion

    //region DiffCallback
    class DiffCallback : DiffUtil.ItemCallback<Tree>() {
        override fun areItemsTheSame(oldItem: Tree, newItem: Tree): Boolean {
            return oldItem.absolutePath == newItem.absolutePath
        }

        override fun areContentsTheSame(oldItem: Tree, newItem: Tree): Boolean {
            return oldItem == newItem && oldItem.children.size == newItem.children.size
        }
    }
    //endregion

    //region OnClickListener
    interface OnClickListener {
        fun onItemClick(view: View)
        fun onSelectedClick(view: View) {
            return
        }
    }
    //endregion
}
